package com.xywl.escort.adapter;

import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywl.escort.R;
import com.xywl.escort.bean.SubscribeOrderResp;
import com.xywl.escort.utils.NumberFormatUtils;
import com.xywl.escort.widget.CircleProgress;
import com.xywl.escort.widget.GlideApp;

import java.util.List;

public class SubscribeOrderAdapter extends BaseQuickAdapter<SubscribeOrderResp.DataBean.ListBean, BaseViewHolder> {
    public SubscribeOrderAdapter(int layoutResId, @Nullable List<SubscribeOrderResp.DataBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, SubscribeOrderResp.DataBean.ListBean item) {
        ImageView imageView = helper.getView(R.id.img_project);
        GlideApp.with(mContext)
                .load(item.coverUrl)
                .centerCrop()
                .placeholder(R.mipmap.icon_default_picture)
                .into(imageView);

        helper.setText(R.id.text_title, item.projectName);
        helper.setText(R.id.text_time, item.orderTime);
        String string = "认购份数 " + item.num + "份";
        SpannableString spannableString = setText(string, string.indexOf("数") + 1, string.length());
        helper.setText(R.id.text_count, spannableString);
        TextView textPrice = helper.getView(R.id.text_price);
        if (null != item.priceYuan) {
            String strPrice = "认购价/份 " + NumberFormatUtils.getInstance().format(item.priceYuan.doubleValue()) + "元";
            SpannableString spannableStringPrice = setText(strPrice, strPrice.indexOf("份") + 1, strPrice.length());
            textPrice.setText(spannableStringPrice);
        }
        LinearLayout layoutTools = helper.getView(R.id.layout_tools);
        Button btnCancelOrder = helper.getView(R.id.btn_cancel_order);
        Button btnDeleteOrder = helper.getView(R.id.btn_delete_order);
        Button btnOrderPayment = helper.getView(R.id.btn_order_payment);
        TextView textSecondLine = helper.getView(R.id.text_second_line);
        switch (item.orderStatus) {
            case 1:
                helper.setText(R.id.text_first_line, "待付款");
                textSecondLine.setVisibility(View.GONE);
                layoutTools.setVisibility(View.VISIBLE);
                btnDeleteOrder.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.VISIBLE);
                btnOrderPayment.setVisibility(View.VISIBLE);
                break;
            case 2:
                helper.setText(R.id.text_first_line, "已付款");
                textSecondLine.setVisibility(View.GONE);
                layoutTools.setVisibility(View.VISIBLE);
                btnDeleteOrder.setVisibility(View.VISIBLE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderPayment.setVisibility(View.GONE);
                break;
            case 3:
                helper.setText(R.id.text_first_line, "支付");
                helper.setText(R.id.text_second_line, "失败");
                textSecondLine.setVisibility(View.VISIBLE);
                layoutTools.setVisibility(View.VISIBLE);
                btnDeleteOrder.setVisibility(View.VISIBLE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderPayment.setVisibility(View.GONE);
                break;
            case 4:
                helper.setText(R.id.text_first_line, "订单");
                helper.setText(R.id.text_second_line, "取消");
                textSecondLine.setVisibility(View.VISIBLE);
                layoutTools.setVisibility(View.VISIBLE);
                btnDeleteOrder.setVisibility(View.VISIBLE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderPayment.setVisibility(View.GONE);
                break;
            case 5:
                helper.setText(R.id.text_first_line, "订单");
                helper.setText(R.id.text_second_line, "失效");
                textSecondLine.setVisibility(View.VISIBLE);
                layoutTools.setVisibility(View.VISIBLE);
                btnDeleteOrder.setVisibility(View.VISIBLE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderPayment.setVisibility(View.GONE);
                break;
            case 6:
                helper.setText(R.id.text_first_line, "支付中");
                textSecondLine.setVisibility(View.GONE);
                layoutTools.setVisibility(View.GONE);
                btnDeleteOrder.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderPayment.setVisibility(View.GONE);
                break;
            default:
                helper.setText(R.id.text_first_line, "订单");
                helper.setText(R.id.text_second_line, "失效");
                textSecondLine.setVisibility(View.VISIBLE);
                layoutTools.setVisibility(View.VISIBLE);
                btnDeleteOrder.setVisibility(View.VISIBLE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderPayment.setVisibility(View.GONE);
                break;
        }
        CircleProgress circleProgress = helper.getView(R.id.circleProgress);
        circleProgress.setMax(100);
        circleProgress.setProgress(100);

        helper.addOnClickListener(R.id.btn_delete_order);
        helper.addOnClickListener(R.id.btn_cancel_order);
        helper.addOnClickListener(R.id.btn_order_payment);
        helper.addOnClickListener(R.id.image_more);
    }

    private SpannableString setText(String text, int start, int end) {
        SpannableString spanString = new SpannableString(text);
        //再构造一个改变字体颜色的Span
        ForegroundColorSpan span = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.aid_prompt_color));
        //将这个Span应用于指定范围的字体
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        AbsoluteSizeSpan span1 = new AbsoluteSizeSpan(15, true);
        spanString.setSpan(span1, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }
}
